//
//  EntryViewController.m
//  MusicPlayer
//
//  Created by dllo on 15/5/26.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "EntryViewController.h"
#import "EntryDetailViewController.h"
#import "UMSocial.h"
#import "NovelDataBase.h"
#import "NovelModel.h"
#import "state.h"

@interface EntryViewController ()
@property (nonatomic, retain) UITextField *nameText;
@end

@implementation EntryViewController

- (void)dealloc
{
    [_nameText release];
    [super dealloc];
}

-(void)loadView
{
    [super loadView];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    UIBarButtonItem *myInfo = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(myInfoAction:)];
    myInfo.tintColor = [UIColor redColor];
    self.navigationItem.leftBarButtonItem = myInfo;
    
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 170, 60, 30)];
    nameLabel.text = @"用户名";
    [self.view addSubview:nameLabel];
    
    self.nameText = [[UITextField alloc] initWithFrame:CGRectMake(120, 170, 200, 30)];
    _nameText.placeholder = @"请输入用户名";
    _nameText.borderStyle = 3;
//    nameText.layer.borderWidth = 2;
    [self.view addSubview:_nameText];
    
    
    UILabel *miLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 230, 60, 30)];
    miLabel.text = @"密码";
    [self.view addSubview:miLabel];
    
    UITextField *miText = [[UITextField alloc] initWithFrame:CGRectMake(120, 230, 200, 30)];
    miText.placeholder = @"请输入用户名";
    miText.borderStyle = 3;
    //    nameText.layer.borderWidth = 2;
    [self.view addSubview:miText];
    
    UIButton *pin = [[UIButton alloc] initWithFrame:CGRectMake(60, 280, 100, 30)];
    
    
     [pin.layer setBorderWidth:1];
    pin.layer.cornerRadius = 9;

    
    [pin.layer setBorderColor:[UIColor blueColor].CGColor];
    [pin setTitle:@"注册" forState:UIControlStateNormal];
    [pin setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    pin.titleLabel.backgroundColor = [UIColor purpleColor];
    [pin addTarget:self action:@selector(pinAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:pin];
    
    
    UIButton *ent = [[UIButton alloc] initWithFrame:CGRectMake(200, 280, 100, 30)];

    [ent.layer setBorderWidth:1];
    ent.layer.cornerRadius = 9;
    [ent.layer setBorderColor:[UIColor blueColor].CGColor];
    [ent setTitle:@"登陆" forState:UIControlStateNormal];
    [ent setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [ent addTarget:self action:@selector(entAction:) forControlEvents:UIControlEventTouchUpInside];
 
    [self.view addSubview:ent];
    
    UIButton *sina = [[UIButton alloc] initWithFrame:CGRectMake(200, 350, 50, 50)];
    [sina setImage:[UIImage imageNamed:@"sina.jpg"] forState:UIControlStateNormal];
    [sina addTarget:self action:@selector(sinaAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sina];

}

- (void)sinaAction:(id)sender
{
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //          获取微博用户名、uid、token等
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            
            
            state *single =[state shareManager ];
            single.login = @"yes";

            [[NSUserDefaults standardUserDefaults] setObject:single.login forKey:@"state"];
            [[NSUserDefaults standardUserDefaults] setObject:snsAccount.userName forKey:@"userName"];

            
        }});
    
    //获取accestoken以及新浪用户信息，得到的数据在回调Block对象形参respone的data属性
    [[UMSocialDataService defaultDataService] requestSnsInformation:UMShareToSina  completion:^(UMSocialResponseEntity *response){
        NSLog(@"SnsInformation is %@",response.data);
    }];
    
}





- (void)pinAction:(id)sender
{
    EntryDetailViewController *detail = [[EntryDetailViewController alloc] init];
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
    
}

- (void)myInfoAction:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}





- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
